package com.jarvis.app.car.adapter;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jarvis.app.car.R;
import com.jarvis.app.car.model.Ticket;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class AnswerTicketAdapter extends RecyclerView.Adapter<AnswerTicketAdapter.AnswerTicketAdapterHolder> {

    private List<Ticket> tickets = new ArrayList<>();

    private AnswerTicketAdapter.ItemViewOnClickListener itemViewOnClickListener;

    public void setItemViewOnClickListener(AnswerTicketAdapter.ItemViewOnClickListener itemViewOnClickListener) {
        this.itemViewOnClickListener = itemViewOnClickListener;
    }

    public List<Ticket> getTickets() {
        return tickets;
    }

    public void setTickets(List<Ticket> tickets) {
        this.tickets = tickets;

        //一定要刷新
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public AnswerTicketAdapter.AnswerTicketAdapterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载布局
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.answer_ticket_list_item, null);
        return new AnswerTicketAdapter.AnswerTicketAdapterHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull AnswerTicketAdapterHolder holder, int position) {
        Ticket ticket = getTickets().get(position);

        holder.tv_ticketSimpleContext.setText(ticket.getIssue());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            holder.tv_ticketTime.setText("时间：" + ticket.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:dd")));
        }
        holder.tv_ticketSimpleContext.setText("问题：" + ticket.getIssue());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Objects.nonNull(itemViewOnClickListener)) {
                    itemViewOnClickListener.infoOnClick(ticket, position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return getTickets().size();
    }


    static class AnswerTicketAdapterHolder extends RecyclerView.ViewHolder {

        TextView tv_ticketTime;

        TextView tv_ticketSimpleContext;


        public AnswerTicketAdapterHolder(@NonNull View itemView) {
            super(itemView);
            tv_ticketTime = itemView.findViewById(R.id.tv_ticketTime);

            tv_ticketSimpleContext = itemView.findViewById(R.id.tv_ticketSimpleContext);


        }
    }

    public interface ItemViewOnClickListener {
        void infoOnClick(Ticket ticket, int position);
    }
}
